﻿using System.Collections.Generic;
using System.Linq;
using Medianamik.Core.Profile;

namespace Medianamik.Core.Security
{
    public static class Extensions
    {
        public static bool IsParent(this Group parentGroup,
            Group childGroup)
        {
            if (parentGroup.ID.Equals(MedianamikGroups.SuperAdmins))
                return true;
           
            return childGroup.ParentID.Equals(parentGroup.ID) || 
                childGroup.GetParentIDs(false).Contains(parentGroup.ID);
        }

        public static IEnumerable<Group> GetChildGroups(this IEnumerable<Group> groups, int depth)
        {
            return groups.SelectMany(g => g.GetChildGroups(depth));
        }

        public static IEnumerable<Group> GetAllChildGroups(this IEnumerable<Group> groups)
        {
            return groups.SelectMany(g => g.GetAllChildGroups());
        }

        public static IEnumerable<Group> GetAllAllowedGroups(this IEnumerable<Group> groups, UserProfile userProfile)
        {
            return userProfile.MayManageOwnGroup ? groups.GetAllChildGroups().Union(groups) : 
                groups.GetAllChildGroups();
        }
    }
}
